package com.msh.artascope.devcloud.service.client;

import com.alibaba.fastjson.JSONObject;
import com.msh.artascope.devcloud.client.po.JenkinsConfigPo;
import com.msh.artascope.devcloud.service.define.DevCloudDef;
import com.msh.artascope.devcloud.service.util.RestTemplateUtil;
import com.msh.frame.client.define.StringDef;
import com.msh.frame.common.util.Base64Util;
import com.msh.frame.common.util.URLCoderUtil;
import java.io.UnsupportedEncodingException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Component
@AllArgsConstructor
public class JenkinsClient {
    private RestTemplate restTemplate;

    public JenkinsClient() {
        this.restTemplate = RestTemplateUtil.newNoCheckSslRestTemplate();
    }

    private static final String GET_JOB_BUILD_LOG_FORMAT = "%s/job/%s/%s/logText/progressiveText/api/json";
    private static final String GET_JOB_BUILD_DETAIL_FORMAT = "%s/job/%s/%s/api/json";
    private static final String GET_JOB_DETAIL_FORMAT = "%s/job/%s/api/json";
    private static final String GET_JOB_BUILD_FORMAT = "%s/job/%s/buildWithParameters/api/json";

    private static final String JOB_CREATE_FORMAT = "%s/createItem/api/json?name=%s";
    private static final String JOB_REMOVE_FORMAT = "%s/job/%s/doDelete/api/json";


    public void createJob(JenkinsConfigPo po, String namespace, String projectName, String xml) throws UnsupportedEncodingException {
      String jobName = DevCloudDef.getJobName(namespace, projectName);
      String url = String.format(JOB_CREATE_FORMAT, po.getAddress(), jobName);
        restTemplate.exchange(url, HttpMethod.POST, buildXmlHttpEntity(po, xml), String.class);
    }

    public void removeJob(JenkinsConfigPo po, String namespace, String projectName) throws UnsupportedEncodingException {
      String jobName = DevCloudDef.getJobName(namespace, projectName);
      String url = String.format(JOB_REMOVE_FORMAT, po.getAddress(), jobName);
        restTemplate.exchange(url, HttpMethod.POST, buildJsonHttpEntity(po), String.class);
    }

    public void build(JenkinsConfigPo po, String namespace, String projectName, MultiValueMap<String, String> params) throws UnsupportedEncodingException {
      String jobName = DevCloudDef.getJobName(namespace, projectName);
      String url = String.format(GET_JOB_BUILD_FORMAT, po.getAddress(), jobName);
        restTemplate.exchange(url + "?" + URLCoderUtil.buildQuery(params), HttpMethod.POST, buildJsonHttpEntity(po), String.class);
    }

    public String log(JenkinsConfigPo po, String namespace, String projectName, Integer number) throws UnsupportedEncodingException {
      String jobName = DevCloudDef.getJobName(namespace, projectName);
      String url = String.format(GET_JOB_BUILD_LOG_FORMAT, po.getAddress(), jobName, number);
        return restTemplate.exchange(url, HttpMethod.GET, buildJsonHttpEntity(po), String.class).getBody();
    }

    public ConsoleLog logInfo(JenkinsConfigPo po, String namespace, String projectName, Integer number, Integer start) throws UnsupportedEncodingException {
      String jobName = DevCloudDef.getJobName(namespace, projectName);
      String url = String.format(GET_JOB_BUILD_LOG_FORMAT, po.getAddress(), jobName, number);
        MultiValueMap map = new LinkedMultiValueMap();
        map.add("start", start);
        ResponseEntity<String> entity = restTemplate.exchange(url, HttpMethod.POST, buildFormHttpEntity(po, map), String.class);
        String logStr = entity.getBody();
        if(null == logStr){
            logStr = StringDef.EMPTY;
        }
        HttpHeaders headers = entity.getHeaders();
        String moreData = headers.getFirst("x-more-data");
        String textSize = headers.getFirst("x-text-size");
        boolean hasMoreData = false;
        int currentBufferSize = 0;
        if (moreData != null) {
            hasMoreData = Boolean.TRUE.toString().equals(moreData);
        }

        if (textSize != null) {
            try {
                currentBufferSize = Integer.parseInt(textSize);
            } catch (NumberFormatException var11) {
                log.warn("无法获取日志信息, job: {}, number: {}!", jobName, number);
            }
        }

        return new ConsoleLog(logStr, hasMoreData, currentBufferSize);
    }

    /**
     * {
     *     "_class": "org.jenkinsci.plugins.workflow.job.WorkflowJob",
     *     "actions": [
     *         {},
     *         {},
     *         {},
     *         {},
     *         {},
     *         {},
     *         {},
     *         {
     *             "_class": "org.jenkinsci.plugins.displayurlapi.actions.JobDisplayAction"
     *         },
     *         {},
     *         {},
     *         {
     *             "_class": "com.cloudbees.plugins.credentials.ViewCredentialsAction"
     *         }
     *     ],
     *     "description": "",
     *     "displayName": "dev_devcloud-server",
     *     "displayNameOrNull": null,
     *     "fullDisplayName": "dev_devcloud-server",
     *     "fullName": "dev_devcloud-server",
     *     "name": "dev_devcloud-server",
     *     "url": "http://jk.msh.com/job/dev_devcloud-server/",
     *     "buildable": true,
     *     "builds": [
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 20,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/20/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 19,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/19/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 18,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/18/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 17,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/17/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 16,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/16/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 15,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/15/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 14,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/14/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 13,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/13/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 12,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/12/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 11,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/11/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 10,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/10/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 9,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/9/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 8,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/8/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 7,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/7/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 6,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/6/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 5,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/5/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 4,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/4/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 3,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/3/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 2,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/2/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 1,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/1/"
     *         }
     *     ],
     *     "color": "blue",
     *     "firstBuild": {
     *         "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *         "number": 1,
     *         "url": "http://jk.msh.com/job/dev_devcloud-server/1/"
     *     },
     *     "healthReport": [
     *         {
     *             "description": "Build stability: No recent builds failed.",
     *             "iconClassName": "icon-health-80plus",
     *             "iconUrl": "health-80plus.png",
     *             "score": 100
     *         }
     *     ],
     *     "inQueue": false,
     *     "keepDependencies": false,
     *     "lastBuild": {
     *         "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *         "number": 20,
     *         "url": "http://jk.msh.com/job/dev_devcloud-server/20/"
     *     },
     *     "lastCompletedBuild": {
     *         "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *         "number": 20,
     *         "url": "http://jk.msh.com/job/dev_devcloud-server/20/"
     *     },
     *     "lastFailedBuild": {
     *         "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *         "number": 12,
     *         "url": "http://jk.msh.com/job/dev_devcloud-server/12/"
     *     },
     *     "lastStableBuild": {
     *         "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *         "number": 20,
     *         "url": "http://jk.msh.com/job/dev_devcloud-server/20/"
     *     },
     *     "lastSuccessfulBuild": {
     *         "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *         "number": 20,
     *         "url": "http://jk.msh.com/job/dev_devcloud-server/20/"
     *     },
     *     "lastUnstableBuild": null,
     *     "lastUnsuccessfulBuild": {
     *         "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *         "number": 12,
     *         "url": "http://jk.msh.com/job/dev_devcloud-server/12/"
     *     },
     *     "nextBuildNumber": 21,
     *     "property": [
     *         {
     *             "_class": "hudson.model.ParametersDefinitionProperty",
     *             "parameterDefinitions": [
     *                 {
     *                     "_class": "hudson.model.StringParameterDefinition",
     *                     "defaultParameterValue": {
     *                         "_class": "hudson.model.StringParameterValue",
     *                         "name": "projectGitUrl",
     *                         "value": ""
     *                     },
     *                     "description": "项目git代码地址",
     *                     "name": "projectGitUrl",
     *                     "type": "StringParameterDefinition"
     *                 },
     *                 {
     *                     "_class": "hudson.model.StringParameterDefinition",
     *                     "defaultParameterValue": {
     *                         "_class": "hudson.model.StringParameterValue",
     *                         "name": "branch",
     *                         "value": ""
     *                     },
     *                     "description": "分支版本",
     *                     "name": "branch",
     *                     "type": "StringParameterDefinition"
     *                 },
     *                 {
     *                     "_class": "hudson.model.StringParameterDefinition",
     *                     "defaultParameterValue": {
     *                         "_class": "hudson.model.StringParameterValue",
     *                         "name": "moduleName",
     *                         "value": ""
     *                     },
     *                     "description": "构建模块名",
     *                     "name": "moduleName",
     *                     "type": "StringParameterDefinition"
     *                 },
     *                 {
     *                     "_class": "hudson.model.StringParameterDefinition",
     *                     "defaultParameterValue": {
     *                         "_class": "hudson.model.StringParameterValue",
     *                         "name": "pullImageName",
     *                         "value": ""
     *                     },
     *                     "description": "镜像地址",
     *                     "name": "pullImageName",
     *                     "type": "StringParameterDefinition"
     *                 },
     *                 {
     *                     "_class": "hudson.model.StringParameterDefinition",
     *                     "defaultParameterValue": {
     *                         "_class": "hudson.model.StringParameterValue",
     *                         "name": "commandParam",
     *                         "value": ""
     *                     },
     *                     "description": "命令参数",
     *                     "name": "commandParam",
     *                     "type": "StringParameterDefinition"
     *                 },
     *                 {
     *                     "_class": "hudson.model.StringParameterDefinition",
     *                     "defaultParameterValue": {
     *                         "_class": "hudson.model.StringParameterValue",
     *                         "name": "pipelineName",
     *                         "value": ""
     *                     },
     *                     "description": "pipeline脚本",
     *                     "name": "pipelineName",
     *                     "type": "StringParameterDefinition"
     *                 },
     *                 {
     *                     "_class": "hudson.model.StringParameterDefinition",
     *                     "defaultParameterValue": {
     *                         "_class": "hudson.model.StringParameterValue",
     *                         "name": "javaArgs",
     *                         "value": ""
     *                     },
     *                     "description": "java命令行参数",
     *                     "name": "javaArgs",
     *                     "type": "StringParameterDefinition"
     *                 },
     *                 {
     *                     "_class": "hudson.model.StringParameterDefinition",
     *                     "defaultParameterValue": {
     *                         "_class": "hudson.model.StringParameterValue",
     *                         "name": "scriptGitUrl",
     *                         "value": ""
     *                     },
     *                     "description": "脚本git地址",
     *                     "name": "scriptGitUrl",
     *                     "type": "StringParameterDefinition"
     *                 },
     *                 {
     *                     "_class": "hudson.model.StringParameterDefinition",
     *                     "defaultParameterValue": {
     *                         "_class": "hudson.model.StringParameterValue",
     *                         "name": "scriptBranch",
     *                         "value": ""
     *                     },
     *                     "description": "脚本分支",
     *                     "name": "scriptBranch",
     *                     "type": "StringParameterDefinition"
     *                 }
     *             ]
     *         }
     *     ],
     *     "queueItem": null,
     *     "concurrentBuild": true,
     *     "resumeBlocked": false
     * }
     * @param po
     * @param namespace
     * @param projectName
     * @param number
     * @return
     * @throws UnsupportedEncodingException
     */
    public JSONObject getJobBuildDetail(JenkinsConfigPo po, String namespace, String projectName, Integer number) throws UnsupportedEncodingException {
      String jobName = DevCloudDef.getJobName(namespace, projectName);
      String url = String.format(GET_JOB_BUILD_DETAIL_FORMAT, po.getAddress(), jobName, number);
        try {
            return restTemplate.exchange(url, HttpMethod.GET, buildJsonHttpEntity(po), JSONObject.class).getBody();
        } catch (HttpClientErrorException.NotFound e) {
            return null;
        }
    }

    /**
     * {
     *     "_class": "org.jenkinsci.plugins.workflow.job.WorkflowJob",
     *     "actions": [
     *         {},
     *         {},
     *         {},
     *         {},
     *         {},
     *         {},
     *         {},
     *         {
     *             "_class": "org.jenkinsci.plugins.displayurlapi.actions.JobDisplayAction"
     *         },
     *         {},
     *         {},
     *         {
     *             "_class": "com.cloudbees.plugins.credentials.ViewCredentialsAction"
     *         }
     *     ],
     *     "description": "",
     *     "displayName": "dev_devcloud-server",
     *     "displayNameOrNull": null,
     *     "fullDisplayName": "dev_devcloud-server",
     *     "fullName": "dev_devcloud-server",
     *     "name": "dev_devcloud-server",
     *     "url": "http://jk.msh.com/job/dev_devcloud-server/",
     *     "buildable": true,
     *     "builds": [
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 20,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/20/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 19,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/19/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 18,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/18/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 17,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/17/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 16,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/16/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 15,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/15/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 14,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/14/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 13,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/13/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 12,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/12/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 11,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/11/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 10,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/10/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 9,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/9/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 8,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/8/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 7,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/7/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 6,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/6/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 5,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/5/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 4,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/4/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 3,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/3/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 2,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/2/"
     *         },
     *         {
     *             "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *             "number": 1,
     *             "url": "http://jk.msh.com/job/dev_devcloud-server/1/"
     *         }
     *     ],
     *     "color": "blue",
     *     "firstBuild": {
     *         "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *         "number": 1,
     *         "url": "http://jk.msh.com/job/dev_devcloud-server/1/"
     *     },
     *     "healthReport": [
     *         {
     *             "description": "Build stability: No recent builds failed.",
     *             "iconClassName": "icon-health-80plus",
     *             "iconUrl": "health-80plus.png",
     *             "score": 100
     *         }
     *     ],
     *     "inQueue": false,
     *     "keepDependencies": false,
     *     "lastBuild": {
     *         "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *         "number": 20,
     *         "url": "http://jk.msh.com/job/dev_devcloud-server/20/"
     *     },
     *     "lastCompletedBuild": {
     *         "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *         "number": 20,
     *         "url": "http://jk.msh.com/job/dev_devcloud-server/20/"
     *     },
     *     "lastFailedBuild": {
     *         "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *         "number": 12,
     *         "url": "http://jk.msh.com/job/dev_devcloud-server/12/"
     *     },
     *     "lastStableBuild": {
     *         "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *         "number": 20,
     *         "url": "http://jk.msh.com/job/dev_devcloud-server/20/"
     *     },
     *     "lastSuccessfulBuild": {
     *         "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *         "number": 20,
     *         "url": "http://jk.msh.com/job/dev_devcloud-server/20/"
     *     },
     *     "lastUnstableBuild": null,
     *     "lastUnsuccessfulBuild": {
     *         "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
     *         "number": 12,
     *         "url": "http://jk.msh.com/job/dev_devcloud-server/12/"
     *     },
     *     "nextBuildNumber": 21,
     *     "property": [
     *         {
     *             "_class": "hudson.model.ParametersDefinitionProperty",
     *             "parameterDefinitions": [
     *                 {
     *                     "_class": "hudson.model.StringParameterDefinition",
     *                     "defaultParameterValue": {
     *                         "_class": "hudson.model.StringParameterValue",
     *                         "name": "projectGitUrl",
     *                         "value": ""
     *                     },
     *                     "description": "项目git代码地址",
     *                     "name": "projectGitUrl",
     *                     "type": "StringParameterDefinition"
     *                 },
     *                 {
     *                     "_class": "hudson.model.StringParameterDefinition",
     *                     "defaultParameterValue": {
     *                         "_class": "hudson.model.StringParameterValue",
     *                         "name": "branch",
     *                         "value": ""
     *                     },
     *                     "description": "分支版本",
     *                     "name": "branch",
     *                     "type": "StringParameterDefinition"
     *                 },
     *                 {
     *                     "_class": "hudson.model.StringParameterDefinition",
     *                     "defaultParameterValue": {
     *                         "_class": "hudson.model.StringParameterValue",
     *                         "name": "moduleName",
     *                         "value": ""
     *                     },
     *                     "description": "构建模块名",
     *                     "name": "moduleName",
     *                     "type": "StringParameterDefinition"
     *                 },
     *                 {
     *                     "_class": "hudson.model.StringParameterDefinition",
     *                     "defaultParameterValue": {
     *                         "_class": "hudson.model.StringParameterValue",
     *                         "name": "pullImageName",
     *                         "value": ""
     *                     },
     *                     "description": "镜像地址",
     *                     "name": "pullImageName",
     *                     "type": "StringParameterDefinition"
     *                 },
     *                 {
     *                     "_class": "hudson.model.StringParameterDefinition",
     *                     "defaultParameterValue": {
     *                         "_class": "hudson.model.StringParameterValue",
     *                         "name": "commandParam",
     *                         "value": ""
     *                     },
     *                     "description": "命令参数",
     *                     "name": "commandParam",
     *                     "type": "StringParameterDefinition"
     *                 },
     *                 {
     *                     "_class": "hudson.model.StringParameterDefinition",
     *                     "defaultParameterValue": {
     *                         "_class": "hudson.model.StringParameterValue",
     *                         "name": "pipelineName",
     *                         "value": ""
     *                     },
     *                     "description": "pipeline脚本",
     *                     "name": "pipelineName",
     *                     "type": "StringParameterDefinition"
     *                 },
     *                 {
     *                     "_class": "hudson.model.StringParameterDefinition",
     *                     "defaultParameterValue": {
     *                         "_class": "hudson.model.StringParameterValue",
     *                         "name": "javaArgs",
     *                         "value": ""
     *                     },
     *                     "description": "java命令行参数",
     *                     "name": "javaArgs",
     *                     "type": "StringParameterDefinition"
     *                 },
     *                 {
     *                     "_class": "hudson.model.StringParameterDefinition",
     *                     "defaultParameterValue": {
     *                         "_class": "hudson.model.StringParameterValue",
     *                         "name": "scriptGitUrl",
     *                         "value": ""
     *                     },
     *                     "description": "脚本git地址",
     *                     "name": "scriptGitUrl",
     *                     "type": "StringParameterDefinition"
     *                 },
     *                 {
     *                     "_class": "hudson.model.StringParameterDefinition",
     *                     "defaultParameterValue": {
     *                         "_class": "hudson.model.StringParameterValue",
     *                         "name": "scriptBranch",
     *                         "value": ""
     *                     },
     *                     "description": "脚本分支",
     *                     "name": "scriptBranch",
     *                     "type": "StringParameterDefinition"
     *                 }
     *             ]
     *         }
     *     ],
     *     "queueItem": null,
     *     "concurrentBuild": true,
     *     "resumeBlocked": false
     * }
     * @param po
     * @param namespace
     * @param projectName
     * @return
     * @throws UnsupportedEncodingException
     */
    public JSONObject getJobDetail(JenkinsConfigPo po, String namespace, String projectName) throws UnsupportedEncodingException {
      String jobName = DevCloudDef.getJobName(namespace, projectName);
      String url = String.format(GET_JOB_DETAIL_FORMAT, po.getAddress(), jobName);
        try {
            return restTemplate.exchange(url, HttpMethod.GET, buildJsonHttpEntity(po), JSONObject.class).getBody();
        } catch (HttpClientErrorException.NotFound e) {
            return null;
        }
    }


    private HttpEntity<String> buildXmlHttpEntity(JenkinsConfigPo po, String xml) throws UnsupportedEncodingException {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Basic " + Base64Util.encrypt(po.getUsername()+":" + po.getPassword()));
        headers.add("Content-Type", "application/xml");
        headers.setContentType(MediaType.APPLICATION_XML);
        return new HttpEntity<>(xml, headers);
    }
    private HttpEntity<String> buildJsonHttpEntity(JenkinsConfigPo po) throws UnsupportedEncodingException {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Basic " + Base64Util.encrypt(po.getUsername()+":" + po.getPassword()));
        headers.setContentType(MediaType.APPLICATION_JSON);
        return new HttpEntity<>(headers);
    }
    private HttpEntity<MultiValueMap> buildFormHttpEntity(JenkinsConfigPo po, MultiValueMap map) throws UnsupportedEncodingException {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Basic " + Base64Util.encrypt(po.getUsername()+":" + po.getPassword()));
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        return new HttpEntity<>(map, headers);
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class ConsoleLog{
        private String log;
        private Boolean hasMoreData;
        private Integer currentBufferSize;
    }
}
